package cn.qixin.passport.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.Random;

/**
 * 邮箱验证码
 */
public class Captcha {
    //定义方法随机生成5位验证码返回
    public static String CheckCode(int n) {
        Random r = new Random();
        //设置变量接收生成验证码
        String code = "";
        //定义for循环  循环n次 依次生成随机字符
        for (int i = 0; i <= n; i++) {
            int type = r.nextInt(3); //0 1 2
            switch (type) {
                case 0:
                    //大写字母(A 65 - Z 65+25)(0-25)+65
                    //char ch = (char)(r.nextInt(26)+65);
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    //小写   97 - 122
                    char ch2 = (char) (r.nextInt(26) + 97);
                    code += ch2;
                    break;
                case 2:
                    //数字
                    int ch3 = r.nextInt(10);  //数字不需要强转char
                    code += ch3;
                    break;
            }
        }
        return code;
    }

}
